package pers.gxm.iterator;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class BookShelf implements Iterable<Book> {
    private List<Book> books = new ArrayList<>();
    /**
     *  记录即将放入的书的索引
     */
    private int last = 0;

    public Book getBookAt(int index){
        if(index < 0){
            throw new RuntimeException("索引为负数请确认");
        }
        if(index >= books.size()){
            throw new RuntimeException("超限");
        }
        return books.get(index);
    }
    public void appendBook(Book book){
        books.add(book);
        last++;
    }

    public int getLength(){
        return books.size();
    }


    @Override
    public Iterator<Book> iterator() {
        return new BookShelfIterator(this);
    }
}
